<template>
  <el-dialog title="选择等级" :visible.sync="localOpen" width="800px":close-on-click-modal="true" @close="cancel"
    append-to-body>
    <div>
      <el-form :model="selectAgentGradeQueryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
        label-width="68px">
        <el-form-item label="等级名称" prop="gradeName">
          <el-input v-model="selectAgentGradeQueryParams.gradeName" placeholder="请输入等级名称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" ref="selectAgentGradeTable" :data="gradeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="等级名称" align="center" prop="gradeName" />
        <el-table-column label="等级code" align="center" prop="gradeCode" />
        <el-table-column label="招生人数" align="center" prop="gradeCount" />
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="selectAgentGradeQueryParams.pageNum"
        :limit.sync="selectAgentGradeQueryParams.pageSize" @pagination="getList" />
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="selectGradeData">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    listGrade
  } from "@/api/bus_web/agent/grade";
  import store from "@/store";
  import {
    mapState
  } from 'vuex';

  export default {
    name: "SelectGrade",
    props: {
      open: Boolean
    },
    data() {
      return {
        localOpen: this.open,
        // 遮罩层
        loading: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 代理商等级表格数据
        gradeList: [],
        selectAgentGradeTable:[],
        // 弹出层标题
        title: "",
        // 查询参数
        selectAgentGradeQueryParams: {
          pageNum: 1,
          pageSize: 10,
          gradeName: null,
          gradeCode: null,
        },
        // 表单参数
        form: {}
      };
    },
    watch: {
      open(newValue, oldValue) {
        this.localOpen = newValue; // 同步父组件的 open 值到本地状态
      },
      localOpen(newValue) {
        if (newValue) {
          this.rows = []
          this.getList() // 当本地状态变为 false 时，通知父组件
        } else {
          this.cancel()
        }
      }
    },
    computed: {
      ...mapState({
        agentGrade: state => state.selectAgentGrade.grade,
        gradeSelectModel: state => state.selectAgentGrade.gradeSelectModel
      })
    },
    methods: {
      /** 查询代理商等级列表 */
      getList() {
        this.loading = true;
        listGrade(this.selectAgentGradeQueryParams).then(response => {
          this.gradeList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.reset()
        this.$emit('close-select-window')
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          gradeName: null,
          gradeCode: null,
          gradeCount: null,
          gmtCreate: null,
          gmtModify: null,
          deleted: null,
          createBy: null,
          updateBy: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.selectAgentGradeQueryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        if(selection.length>1 && this.gradeSelectModel==2){
          selection=[]
          this.$refs.selectAgentGradeTable.clearSelection()
          this.$store.dispatch('selectAgentGrade/cleanAgentGradeData')
          this.$modal.msgError("最多只能选择1条数据")
          return
        }
        selection.map(item => {
          this.$store.dispatch('selectAgentGrade/setAgentGrade', item)
        })
      },
      //确定选择数据
      selectGradeData() {
        if (this.agentGrade.length < 1) {
          this.$modal.msgError("请至少选中1条等级")
          return
        }
        this.$emit('selected-data')
      },
    }
  }
</script>
